﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 位置结构体
/// </summary>
public struct CubePos 
{
   public int x;//0-3
   public int y;//0-3
   public static bool operator ==(CubePos lhs, CubePos rhs)
   {
       if (lhs.x == rhs.x && lhs.y == rhs.y)
           return true;
       else
           return false;
   }
   public static bool operator !=(CubePos lhs, CubePos rhs)
   {
       if (lhs.x != rhs.x || lhs.y != rhs.y)
           return true;
       else
           return false;
   }
    public static CubePos operator +(CubePos lhs, CubePos rhs)
   {
        CubePos p=new CubePos();
        p.x=lhs.x+rhs.x;
        p.y=lhs.y+rhs.y;
        return p;
   }

}    
public class NumCube : MonoBehaviour
{
    //材质组
    public Material[] colors ;
    //全体列表
    public static List<NumCube> cubes = new List<NumCube>();
    //位置
    private CubePos _pos;
    public CubePos pos 
    { 
        get{return _pos;} 
        set 
        { 
            transform.position = new Vector3(value.x, value.y); 
            _pos = value; 
        } 
    }
    //移动判断
    public static bool moved = false;

    /// <summary>
    /// 得到一个空的位置
    /// </summary>
    /// <returns></returns>
    CubePos getRandomPos()
    {
        CubePos p=new CubePos();
        do
        {
            p.x = Random.Range(0, 4);
            p.y = Random.Range(0, 4);
        } while (!isNullPos(p));
        
        return p;
    }
    /// <summary>
    ///测试一个位置合法的位置,出界返回false
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    bool isNullPos(CubePos p)
    {
        foreach (NumCube c in cubes)
        {
            if (c.pos == p)
            {
                return false;
            }
        }
        //出界测试,出界返回false
        if (p.x < 0 || p.x > 3) return false;
        if (p.y < 0 || p.y > 3) return false;
        return true;
    }
    /// <summary>
    /// 游戏开始
    /// </summary>
    void Start()
    {
        //向列表中加入自已
        cubes.Add(this);
        //随机出数字
        num = Random.value < 0.9f ? 2 : 4;
        //随机出位置
        pos = getRandomPos();

    }
    /// <summary>
    /// 得到位置上的方块
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    NumCube getCube(CubePos p)
    {
        foreach (NumCube c in cubes)
        {
            if (c.pos == p)
            {
                return c;
            }
        }
        return null;
    }
    /// <summary>
    /// 移动实现
    /// </summary>
    /// <param name="p"></param>
    public void move(CubePos p)
    {
        CubePos ppos = pos;
        ppos += p;
        while(isNullPos(ppos))
        {
            moved = true;
            //移动
            pos = ppos;
            //测试下一格
            ppos += p;
        }
        //测试能不能合并
        NumCube temp=getCube(ppos);
        if (temp != null&&temp.num==num)//可以合并
        {
            moved = true;
            //消失自己让对方加大.
            temp.num += num;
            //加分
            GameManager.Inst.score += num * 2;
            cubes.Remove(this);
            Destroy(gameObject);
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (num == 2048)
        {
            //win
            Debug.Log("game win");
        }

    }
    /// <summary>
    /// 自己的值
    /// </summary>
    private int _num;
    private int num
    {
        get { return _num; }
        set
        {
            //改变材质
            foreach (Material m in colors)
            {
                if (m.name == value.ToString())
                {
                    renderer.material = m;
                    name = value.ToString();
                }
            }
            _num = value;
        } 
    }
}
